﻿namespace WMS6.Web.Common
{
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Odbc;
    using System.Data.OleDb;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.LoginServiceRef;
    using WMS6.Web.OrderMgtServiceRef;

    public class ImporData : BasePage
    {
        protected Button btnImport;
        protected HtmlGenericControl divMsg;
        protected HtmlForm form1;
        protected HtmlInputHidden hdnActionFrom;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnLang;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnRtnOrderID;
        protected HtmlInputHidden hdnUserName;
        protected HtmlImage img;
        protected HtmlImage img1;
        protected Label Label1;
        protected Label lblIsUpdate;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblVendorID;
        protected HtmlLink mycss;
        public string projectID;
        protected RadioButton rbtnFalse;
        protected RadioButton rbtnTrue;
        protected FileUpload SelectFile;
        public string strOrderID;
        public string strOwnerID;
        protected HtmlInputButton Submit1;
        protected TextBox txtOwnerID;
        protected TextBox txtVendorID;
        public string UserName;

        protected void btnDown_Click(object sender, EventArgs e)
        {
        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                HttpPostedFile userPostedFile;
                string StrIsUpdate;
                StreamReader sr;
                DataSet ds;
                DataTable dt;
                string strvalue;
                SalesOrderInfo salesOrderInfo;
                this.lblMsg.Text = "";
                this.divMsg.InnerHtml = "";
                if (Path.GetExtension(this.SelectFile.FileName) == ".csv")
                {
                    List<string> strs;
                    string tempStr;
                    userPostedFile = base.Request.Files["SelectFile"];
                    StrIsUpdate = string.Empty;
                    if (this.rbtnTrue.Checked)
                    {
                        StrIsUpdate = "Y";
                    }
                    else
                    {
                        StrIsUpdate = "N";
                    }
                    sr = new StreamReader(userPostedFile.InputStream, Encoding.Default);
                    if (string.Compare(this.hdnActionFrom.Value, "SHIPPINGORDER", true) == 0)
                    {
                        if (!string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
                        {
                            strs = new List<string>();
                            while (sr.Peek() > 0)
                            {
                                tempStr = sr.ReadLine();
                                strs.Add(tempStr);
                            }
                            ds = new DataSet();
                            dt = this.GetDataTable(strs, this.txtOwnerID.Text.Trim());
                            if (dt == null)
                            {
                                this.lblMsg.Text = "模板数据有误!";
                            }
                            else
                            {
                                ds.Tables.Add(dt);
                                strvalue = CommoneMethodServiceClientProxy.GetInstance().ImportByDataSet(ds, this.hdnActionFrom.Value);
                                if (strvalue.StartsWith("Error:", true, null))
                                {
                                    this.divMsg.InnerHtml = strvalue;
                                }
                                else
                                {
                                    this.hdnRtnOrderID.Value = strvalue;
                                    if (!string.IsNullOrEmpty(strvalue))
                                    {
                                        this.divMsg.InnerHtml = strvalue;
                                    }
                                    else
                                    {
                                        this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                                    }
                                }
                                if (!string.IsNullOrEmpty(strvalue))
                                {
                                    this.divMsg.InnerHtml = strvalue;
                                }
                                else
                                {
                                    this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                                }
                            }
                        }
                        else
                        {
                            this.lblMsg.Text = "须输入货主代码!";
                        }
                    }
                    else if (string.Compare(this.hdnActionFrom.Value, "SALESORDERDETAIL", true) == 0)
                    {
                        strs = new List<string>();
                        while (sr.Peek() > 0)
                        {
                            tempStr = sr.ReadLine();
                            strs.Add(tempStr);
                        }
                        ds = new DataSet();
                        dt = this.GetSalesOrderDetailDataTable(strs, base.Request.QueryString["ownerID"], base.Request.QueryString["customerID"]);
                        if (dt == null)
                        {
                            this.lblMsg.Text = "模板数据有误!";
                        }
                        else
                        {
                            ds.Tables.Add(dt);
                            salesOrderInfo = OrderMgtServiceClientProxy.GetInstance().ImportSoByDataSet(ds);
                            if (!(string.IsNullOrEmpty(salesOrderInfo.Remark) || !salesOrderInfo.Remark.StartsWith("Error:", true, null)))
                            {
                                this.divMsg.InnerHtml = salesOrderInfo.Remark;
                            }
                            else
                            {
                                this.divMsg.InnerHtml = "数据导入成功!";
                                this.Session["SessionsalesOrderInfo"] = salesOrderInfo;
                            }
                        }
                    }
                    else
                    {
                        tempStr = sr.ReadToEnd();
                        strvalue = CommoneMethodServiceClientProxy.GetInstance().ImportByData(this.NCSV(tempStr), StrIsUpdate, this.hdnActionFrom.Value);
                        if (!string.IsNullOrEmpty(strvalue))
                        {
                            this.divMsg.InnerHtml = strvalue;
                            this.btnImport.Enabled = false;
                        }
                        else
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                        }
                    }
                }
                else if (Path.GetExtension(this.SelectFile.FileName) == ".xls")
                {
                    StrIsUpdate = string.Empty;
                    if (this.rbtnTrue.Checked)
                    {
                        StrIsUpdate = "Y";
                    }
                    else
                    {
                        StrIsUpdate = "N";
                    }
                    if (string.Compare(this.hdnActionFrom.Value, "SHIPPINGORDER", true) == 0)
                    {
                        if (!string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
                        {
                            ds = new DataSet();
                            dt = this.XLS2DataTable(this.SelectFile.FileContent, this.txtOwnerID.Text.Trim(), "");
                            if (dt == null)
                            {
                                this.lblMsg.Text = "模板数据有误!";
                            }
                            else
                            {
                                ds.Tables.Add(dt);
                                strvalue = CommoneMethodServiceClientProxy.GetInstance().ImportByDataSet(ds, this.hdnActionFrom.Value);
                                if (strvalue.StartsWith("Error:", true, null))
                                {
                                    this.divMsg.InnerHtml = strvalue;
                                }
                                else
                                {
                                    this.hdnRtnOrderID.Value = strvalue;
                                    if (!string.IsNullOrEmpty(strvalue))
                                    {
                                        this.divMsg.InnerHtml = strvalue;
                                    }
                                    else
                                    {
                                        this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                                    }
                                }
                                if (!string.IsNullOrEmpty(strvalue))
                                {
                                    this.divMsg.InnerHtml = strvalue;
                                }
                                else
                                {
                                    this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                                }
                            }
                        }
                        else
                        {
                            this.lblMsg.Text = "须输入货主代码!";
                        }
                    }
                    else if (string.Compare(this.hdnActionFrom.Value, "SALESORDERDETAIL", true) == 0)
                    {
                        ds = new DataSet();
                        dt = this.XLS2DataTable(this.SelectFile.FileContent, base.Request.QueryString["ownerID"], base.Request.QueryString["customerID"]);
                        if (dt == null)
                        {
                            this.lblMsg.Text = "模板数据有误!";
                        }
                        else
                        {
                            ds.Tables.Add(dt);
                            salesOrderInfo = OrderMgtServiceClientProxy.GetInstance().ImportSoByDataSet(ds);
                            if (!(string.IsNullOrEmpty(salesOrderInfo.Remark) || !salesOrderInfo.Remark.StartsWith("Error:", true, null)))
                            {
                                this.divMsg.InnerHtml = salesOrderInfo.Remark;
                            }
                            else
                            {
                                this.divMsg.InnerHtml = "数据导入成功!";
                                this.Session["SessionsalesOrderInfo"] = salesOrderInfo;
                            }
                        }
                    }
                    else if (string.Compare(this.hdnActionFrom.Value, "TRANSFERDETAIL", true) == 0)
                    {
                        ds = new DataSet();
                        dt = this.XLS2DataTable(this.SelectFile.FileContent, "", "");
                        if (dt == null)
                        {
                            this.lblMsg.Text = "模板数据有误!";
                        }
                        else
                        {
                            ds.Tables.Add(dt);
                            strvalue = "";
                            string projectID = base.GetValueFromAppSettings("PROJECT_ID");
                            strvalue = InventoryMgtServiceClientProxy.GetInstance().ImportTransferByDataSet(ds, projectID, this.hdnOrderID.Value);
                            if (strvalue.StartsWith("Error:", true, null))
                            {
                                this.divMsg.InnerHtml = strvalue;
                            }
                            else
                            {
                                this.hdnRtnOrderID.Value = strvalue;
                                if (!string.IsNullOrEmpty(strvalue))
                                {
                                    this.divMsg.InnerHtml = strvalue;
                                }
                                else
                                {
                                    this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                                }
                            }
                            if (this.hdnRtnOrderID.Value == this.hdnOrderID.Value)
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                            }
                            if (!string.IsNullOrEmpty(strvalue))
                            {
                                this.divMsg.InnerHtml = strvalue;
                            }
                            else
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                            }
                        }
                    }
                    else
                    {
                        List<List<string>> tmpData = this.XLS2Array(this.SelectFile.FileContent);
                        strvalue = CommoneMethodServiceClientProxy.GetInstance().ImportByData(tmpData, StrIsUpdate, this.hdnActionFrom.Value);
                        if (!string.IsNullOrEmpty(strvalue))
                        {
                            this.divMsg.InnerHtml = strvalue;
                        }
                        else
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                        }
                    }
                }
                else if (Path.GetExtension(this.SelectFile.FileName) == ".txt")
                {
                    userPostedFile = base.Request.Files["SelectFile"];
                    List<string> data = new List<string>();
                    using (sr = new StreamReader(userPostedFile.InputStream, Encoding.Default))
                    {
                        while (sr.Peek() >= 0)
                        {
                            string sline = sr.ReadLine();
                            if (!string.IsNullOrEmpty(sline))
                            {
                                data.Add(sline);
                            }
                        }
                    }
                    if (string.Compare(this.hdnActionFrom.Value, "RECEIPTS", true) == 0)
                    {
                        if (this.projectID.ToUpper() == "YITAI")
                        {
                            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_001");
                            }
                            else if (string.IsNullOrEmpty(this.txtVendorID.Text.Trim()))
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("F2130_001");
                            }
                            else
                            {
                                strvalue = EDIServiceClientProxy.GetInstance().ASNImportByEDI_YITAI(data, this.txtOwnerID.Text.Trim(), this.txtVendorID.Text.Trim());
                                if (!string.IsNullOrEmpty(strvalue))
                                {
                                    this.divMsg.InnerHtml = strvalue;
                                }
                                else
                                {
                                    this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                                }
                            }
                        }
                    }
                    else if ((string.Compare(this.hdnActionFrom.Value, "SHIPPINGORDERS", true) == 0) && (this.projectID.ToUpper() == "YITAI"))
                    {
                        if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F2110_001");
                        }
                        else
                        {
                            strvalue = EDIServiceClientProxy.GetInstance().OrderImportByEDI_YITAI(data, this.txtOwnerID.Text.Trim());
                            if (!string.IsNullOrEmpty(strvalue))
                            {
                                this.divMsg.InnerHtml = strvalue;
                            }
                            else
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("C015");
                            }
                        }
                    }
                }
                else if (this.projectID.ToUpper() == "YITAI")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C019");
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C016");
                }
            }
            catch (Exception ex)
            {
                this.divMsg.InnerHtml = ex.Message;
            }
        }

        public DataTable GetDataTable(List<string> str, string OwnerID)
        {
            try
            {
                DataTable dt = new DataTable();
                string[] titles = str[0].Split(new char[] { ',' });
                if (titles.Length == 0x1b)
                {
                    int i;
                    dt.Columns.Add("货主");
                    for (i = 0; i < titles.Length; i++)
                    {
                        dt.Columns.Add(titles[i].ToString());
                    }
                    for (i = 1; i < str.Count; i++)
                    {
                        DataRow dr = dt.NewRow();
                        string[] cell = str[i].Split(new char[] { ',' });
                        if (cell.Length != 0x1b)
                        {
                            throw new Exception("数据错误");
                        }
                        dr[0] = OwnerID;
                        for (int j = 0; j < cell.Length; j++)
                        {
                            dr[j + 1] = cell[j];
                        }
                        dt.Rows.Add(dr);
                    }
                }
                if (dt.Rows.Count == 0)
                {
                    dt = null;
                }
                return dt;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public DataTable GetSalesOrderDetailDataTable(List<string> str, string ownerID, string customerID)
        {
            try
            {
                int i;
                DataTable dt = new DataTable();
                string[] titles = str[0].Split(new char[] { ',' });
                dt.Columns.Add("货主");
                dt.Columns.Add("客户");
                for (i = 0; i < titles.Length; i++)
                {
                    if (!string.IsNullOrEmpty(titles[i].ToString().Trim()))
                    {
                        dt.Columns.Add(titles[i].ToString());
                    }
                }
                for (i = 1; i < str.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    string[] cell = str[i].Split(new char[] { ',' });
                    dr[0] = ownerID;
                    dr[1] = customerID;
                    for (int j = 0; j < cell.Length; j++)
                    {
                        dr[j + 2] = cell[j];
                    }
                    dt.Rows.Add(dr);
                }
                if (dt.Rows.Count == 0)
                {
                    dt = null;
                }
                return dt;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public DataSet ImportCSV2DataSet(string filepath)
        {
            DataSet ds = new DataSet();
            string file = Path.GetFileName(filepath);
            string path = Path.GetDirectoryName(filepath) + @"\";
            OdbcConnection conn = new OdbcConnection("Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + path + ";Extensions=csv,tab,txt;");
            try
            {
                OdbcCommand cmd = new OdbcCommand("select * from " + file, conn);
                new OdbcDataAdapter(cmd).Fill(ds);
                conn.Close();
            }
            catch (Exception)
            {
                conn.Close();
            }
            finally
            {
                conn.Close();
            }
            return ds;
        }

        public DataSet ImportExcel2DataSet(string filepath)
        {
            DataSet ds = new DataSet();
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'");
            try
            {
                OleDbCommand cmd = new OleDbCommand("select * from [sheet1$]", conn);
                new OleDbDataAdapter(cmd).Fill(ds);
                conn.Close();
            }
            catch (Exception)
            {
                conn.Close();
            }
            finally
            {
                conn.Close();
            }
            return ds;
        }

        public List<List<string>> NCSV(string str)
        {
            int i;
            bool bwq = false;
            char[] c = str.ToCharArray();
            ArrayList rows = new ArrayList();
            ArrayList cells = new ArrayList();
            string tempStr = string.Empty;
            for (i = 0; i < c.Length; i++)
            {
                switch (c[i])
                {
                    case '"':
                    {
                        if (!bwq)
                        {
                            break;
                        }
                        i++;
                        if (i < c.Length)
                        {
                            if (c[i] == '"')
                            {
                                tempStr = tempStr + c[i];
                            }
                            else if (c[i] == ',')
                            {
                                cells.Add(tempStr);
                                tempStr = string.Empty;
                                bwq = false;
                            }
                            else if (c[i] == '\n')
                            {
                                cells.Add(tempStr);
                                tempStr = string.Empty;
                                bwq = false;
                                rows.Add(cells);
                                cells = new ArrayList();
                            }
                            else
                            {
                                bwq = false;
                            }
                        }
                        else
                        {
                            cells.Add(tempStr);
                            tempStr = string.Empty;
                            rows.Add(cells);
                        }
                        continue;
                    }
                    case ',':
                    {
                        if (bwq)
                        {
                            tempStr = tempStr + c[i];
                        }
                        else
                        {
                            cells.Add(tempStr);
                            tempStr = string.Empty;
                        }
                        continue;
                    }
                    case '\n':
                    {
                        if (c[i - 1] == '\r')
                        {
                            if (c[i - 2] != ',')
                            {
                                cells.Add(tempStr);
                            }
                            rows.Add(cells);
                            cells = new ArrayList();
                            tempStr = string.Empty;
                        }
                        continue;
                    }
                    case '\r':
                    {
                        continue;
                    }
                    default:
                        goto Label_01F8;
                }
                if (tempStr == string.Empty)
                {
                    bwq = true;
                }
                else
                {
                    i++;
                    if (i < c.Length)
                    {
                        tempStr = tempStr + c[i];
                    }
                }
                continue;
            Label_01F8:
                if ((i + 1) > c.Length)
                {
                    tempStr = tempStr + c[i];
                    cells.Add(tempStr);
                    rows.Add(cells);
                    cells = new ArrayList();
                    tempStr = string.Empty;
                }
                else
                {
                    tempStr = tempStr + c[i];
                }
            }
            List<List<string>> arrayList = new List<List<string>>();
            string[][] ret = new string[rows.Count][];
            for (i = 0; i < ret.Length; i++)
            {
                cells = (ArrayList) rows[i];
                List<string> d = new List<string>();
                ret[i] = new string[cells.Count];
                for (int j = 0; j < cells.Count; j++)
                {
                    if ((i == 0) && (j == 0))
                    {
                        d.Add("CREATEDBY");
                        if (!string.IsNullOrEmpty(this.hdnOrderID.Value))
                        {
                            d.Add("ORDERID");
                        }
                        if (!string.IsNullOrEmpty(this.hdnOwnerID.Value))
                        {
                            d.Add("OWNERID");
                        }
                        d.Add("创建人");
                    }
                    else if (j == 0)
                    {
                        d.Add(this.hdnUserName.Value);
                        if (!string.IsNullOrEmpty(this.hdnOrderID.Value))
                        {
                            d.Add(this.hdnOrderID.Value);
                        }
                        if (!string.IsNullOrEmpty(this.hdnOwnerID.Value))
                        {
                            d.Add(this.hdnOwnerID.Value);
                        }
                        d.Add(this.hdnUserName.Value);
                    }
                    d.Add(cells[j].ToString());
                }
                arrayList.Add(d);
            }
            return arrayList;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.projectID = base.GetValueFromAppSettings("PROJECT_ID");
            base.Response.Buffer = true;
            base.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1.0);
            base.Response.Expires = 0;
            base.Response.CacheControl = "no-cache";
            base.Response.AddHeader("Pragma", "No-Cache");
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.hdnUserName.Value = base.UserInfo.UserID;
                PreferenceInfo preferenceInfo = base.UserInfo.Preference;
                this.hdnLang.Value = preferenceInfo.Lang;
                this.hdnActionFrom.Value = base.Request.QueryString["action"].ToUpper();
                this.lblVendorID.Visible = false;
                this.txtVendorID.Visible = false;
                if (string.Compare(this.hdnActionFrom.Value, "SHIPPINGORDER", true) != 0)
                {
                    this.lblOwnerID.Visible = false;
                    this.txtOwnerID.Visible = false;
                }
                if ((string.Compare(this.hdnActionFrom.Value, "RECEIPTS", true) == 0) && (this.projectID.ToUpper() == "YITAI"))
                {
                    this.lblOwnerID.Visible = true;
                    this.txtOwnerID.Visible = true;
                    this.txtOwnerID.Text = base.UserInfo.OwnerID;
                    this.lblVendorID.Visible = true;
                    this.txtVendorID.Visible = true;
                    this.txtVendorID.Text = base.UserInfo.VendorID;
                    this.img.Visible = false;
                    this.img1.Visible = false;
                    this.lblIsUpdate.Visible = false;
                    this.rbtnTrue.Visible = false;
                    this.rbtnFalse.Visible = false;
                }
                if ((string.Compare(this.hdnActionFrom.Value, "SHIPPINGORDERS", true) == 0) && (this.projectID.ToUpper() == "YITAI"))
                {
                    this.lblOwnerID.Visible = true;
                    this.txtOwnerID.Visible = true;
                    this.txtOwnerID.Text = base.UserInfo.OwnerID;
                    this.img.Visible = false;
                    this.img1.Visible = false;
                    this.lblIsUpdate.Visible = false;
                    this.rbtnTrue.Visible = false;
                    this.rbtnFalse.Visible = false;
                }
                if (this.hdnActionFrom.Value == "SKU")
                {
                    if (base.UserInfo.IsGlobal == "N")
                    {
                        this.lblIsUpdate.Visible = false;
                        this.rbtnTrue.Visible = false;
                        this.rbtnFalse.Visible = false;
                    }
                    else
                    {
                        this.lblIsUpdate.Visible = true;
                        this.rbtnTrue.Visible = true;
                        this.rbtnFalse.Visible = true;
                    }
                }
                this.hdnOrderID.Value = base.Request.QueryString["OrderID"];
                this.hdnOwnerID.Value = base.Request.QueryString["OwnerID"];
                this.strOrderID = this.hdnOrderID.Value;
                this.strOwnerID = this.hdnOwnerID.Value;
                string CS40002 = this.hdnActionFrom.Value;
                if ((CS40002 != null) && (((CS40002 == "RECEIPTDETAIL") || (CS40002 == "SHIPPINGORDERDETAIL")) || ((CS40002 == "PURCHASEORDERDETAIL") || (CS40002 == "MOVEMENTDETAIL"))))
                {
                    this.lblIsUpdate.Visible = false;
                    this.rbtnTrue.Visible = false;
                    this.rbtnFalse.Visible = false;
                }
                string csvFileName = string.Empty;
                string excelFileName = string.Empty;
                switch (this.hdnActionFrom.Value)
                {
                    case "CUSTOMER":
                        csvFileName = "Customer.csv";
                        excelFileName = "Customer.xls";
                        break;

                    case "CARRIER":
                        csvFileName = "Carrier.csv";
                        excelFileName = "Carrier.xls";
                        break;

                    case "BILLINGENTITY":
                        csvFileName = "BillingEntity.csv";
                        excelFileName = "BillingEntity.xls";
                        break;

                    case "VENDOR":
                        csvFileName = "Vendor.csv";
                        excelFileName = "Vendor.xls";
                        break;

                    case "SKU":
                        if (!(base.UserInfo.IsGlobal == "Y"))
                        {
                            csvFileName = "SKU_WH.csv";
                            excelFileName = "SKU_WH.xls";
                            break;
                        }
                        csvFileName = "SKU.csv";
                        excelFileName = "SKU.xls";
                        break;

                    case "LOCATION":
                        csvFileName = "Location.csv";
                        excelFileName = "Location.xls";
                        break;

                    case "RECEIPTDETAIL":
                        csvFileName = "ASN.csv";
                        excelFileName = "ASN.xls";
                        break;

                    case "SHIPPINGORDERDETAIL":
                        csvFileName = "SO.csv";
                        excelFileName = "SO.xls";
                        break;

                    case "OWNER":
                        csvFileName = "Owner.csv";
                        excelFileName = "Owner.xls";
                        break;

                    case "ALTERNATESKU":
                        csvFileName = "AlternateSKU.csv";
                        excelFileName = "AlternateSKU.xls";
                        break;

                    case "SHIPPINGORDER":
                        csvFileName = "ShippingOrder.csv";
                        excelFileName = "ShippingOrder.xls";
                        break;

                    case "SALESORDERDETAIL":
                        csvFileName = "SalesOrderDetail.csv";
                        excelFileName = "SalesOrderDetail.xls";
                        this.lblIsUpdate.Visible = false;
                        this.rbtnTrue.Visible = false;
                        this.rbtnFalse.Visible = false;
                        break;

                    case "PURCHASEORDERDETAIL":
                        csvFileName = "PurchaseOrderDetail.csv";
                        excelFileName = "PurchaseOrderDetail.xls";
                        break;

                    case "MOVEMENTDETAIL":
                        csvFileName = "MovementDetail.csv";
                        excelFileName = "MovementDetail.xls";
                        break;

                    case "TRANSFERDETAIL":
                        csvFileName = "TransferDetail.csv";
                        excelFileName = "TransferDetail.xls";
                        this.lblIsUpdate.Visible = false;
                        this.rbtnTrue.Visible = false;
                        this.rbtnFalse.Visible = false;
                        break;

                    case "SHIPPINGCONTAINERDETAIL":
                        csvFileName = "ShippingContainerDetail.csv";
                        excelFileName = "ShippingContainerDetail.xls";
                        this.lblIsUpdate.Visible = false;
                        this.rbtnTrue.Visible = false;
                        this.rbtnFalse.Visible = false;
                        break;

                    case "LPN":
                        csvFileName = "LPN.csv";
                        excelFileName = "LPN.xls";
                        this.lblIsUpdate.Visible = false;
                        this.rbtnTrue.Visible = false;
                        this.rbtnFalse.Visible = false;
                        break;
                }
                this.img.Attributes.Add("title", csvFileName);
                this.img1.Attributes.Add("title", excelFileName);
            }
        }

        public List<List<string>> XLS2Array(Stream stream)
        {
            int i;
            List<List<string>> arrayList = new List<List<string>>();
            HSSFWorkbook workbook = new HSSFWorkbook(stream);
            HSSFSheet sheet = (HSSFSheet) workbook.GetSheetAt(0);
            HSSFRow headerRow = (HSSFRow) sheet.GetRow(0);
            DataTable table = new DataTable();
            int cellCount = headerRow.LastCellNum;
            List<string> header = new List<string> { "CREATEDBY" };
            if (!string.IsNullOrEmpty(this.hdnOrderID.Value))
            {
                header.Add("ORDERID");
            }
            if (!string.IsNullOrEmpty(this.hdnOwnerID.Value))
            {
                header.Add("OWNERID");
            }
            header.Add("创建人");
            for (i = headerRow.FirstCellNum; i < cellCount; i++)
            {
                if ((headerRow.GetCell(i) == null) || (headerRow.GetCell(i).ToString().Trim() == ""))
                {
                    cellCount = i + 1;
                    break;
                }
                header.Add(headerRow.GetCell(i).ToString());
            }
            arrayList.Add(header);
            for (i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
            {
                HSSFRow row = (HSSFRow) sheet.GetRow(i);
                if (((row == null) || (row.GetCell(0) == null)) || (row.GetCell(0).ToString().Trim() == ""))
                {
                    break;
                }
                List<string> data = new List<string> {
                    this.hdnUserName.Value
                };
                if (!string.IsNullOrEmpty(this.hdnOrderID.Value))
                {
                    data.Add(this.hdnOrderID.Value);
                }
                if (!string.IsNullOrEmpty(this.hdnOwnerID.Value))
                {
                    data.Add(this.hdnOwnerID.Value);
                }
                data.Add(this.hdnUserName.Value);
                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) == null)
                    {
                        data.Add("");
                    }
                    else
                    {
                        string s = "";
                        if (row.GetCell(j).CellType == CellType.NUMERIC)
                        {
                            s = row.GetCell(j).NumericCellValue.ToString();
                        }
                        else
                        {
                            s = row.GetCell(j).ToString();
                        }
                        data.Add(s);
                    }
                }
                arrayList.Add(data);
            }
            stream.Close();
            workbook = null;
            sheet = null;
            return arrayList;
        }

        public List<List<string>> XLS2Array(string excelFilePath)
        {
            List<List<string>> arrayList = new List<List<string>>();
            using (FileStream excelFileStream = File.OpenRead(excelFilePath))
            {
                int i;
                HSSFWorkbook workbook = new HSSFWorkbook(excelFileStream);
                HSSFSheet sheet = (HSSFSheet) workbook.GetSheetAt(0);
                HSSFRow headerRow = (HSSFRow) sheet.GetRow(0);
                DataTable table = new DataTable();
                int cellCount = headerRow.LastCellNum;
                List<string> header = new List<string> { "CREATEDBY" };
                if (!string.IsNullOrEmpty(this.hdnOrderID.Value))
                {
                    header.Add("ORDERID");
                }
                if (!string.IsNullOrEmpty(this.hdnOwnerID.Value))
                {
                    header.Add("OWNERID");
                }
                header.Add("创建人");
                for (i = headerRow.FirstCellNum; i < cellCount; i++)
                {
                    if ((headerRow.GetCell(i) == null) || (headerRow.GetCell(i).ToString().Trim() == ""))
                    {
                        cellCount = i + 1;
                        break;
                    }
                    header.Add(headerRow.GetCell(i).ToString());
                }
                arrayList.Add(header);
                for (i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                {
                    HSSFRow row = (HSSFRow) sheet.GetRow(i);
                    if (((row == null) || (row.GetCell(0) == null)) || (row.GetCell(0).ToString().Trim() == ""))
                    {
                        break;
                    }
                    List<string> data = new List<string> {
                        this.hdnUserName.Value
                    };
                    if (!string.IsNullOrEmpty(this.hdnOrderID.Value))
                    {
                        data.Add(this.hdnOrderID.Value);
                    }
                    if (!string.IsNullOrEmpty(this.hdnOwnerID.Value))
                    {
                        data.Add(this.hdnOwnerID.Value);
                    }
                    data.Add(this.hdnUserName.Value);
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) == null)
                        {
                            data.Add("");
                        }
                        else
                        {
                            string s = "";
                            if (row.GetCell(j).CellType == CellType.NUMERIC)
                            {
                                s = row.GetCell(j).NumericCellValue.ToString();
                            }
                            else
                            {
                                s = row.GetCell(j).ToString();
                            }
                            data.Add(s);
                        }
                    }
                    arrayList.Add(data);
                }
                excelFileStream.Close();
                workbook = null;
                sheet = null;
            }
            return arrayList;
        }

        public DataTable XLS2DataTable(Stream stream, string ownerID, string customerID)
        {
            int i;
            HSSFWorkbook workbook = new HSSFWorkbook(stream);
            HSSFSheet sheet = (HSSFSheet) workbook.GetSheetAt(0);
            HSSFRow headerRow = (HSSFRow) sheet.GetRow(0);
            DataTable table = new DataTable();
            int cellCount = headerRow.LastCellNum;
            int colInc = 0;
            if (!string.IsNullOrEmpty(ownerID))
            {
                table.Columns.Add("货主");
                colInc++;
            }
            if (!string.IsNullOrEmpty(customerID))
            {
                table.Columns.Add("客户");
                colInc++;
            }
            for (i = headerRow.FirstCellNum; i < cellCount; i++)
            {
                if ((headerRow.GetCell(i) == null) || (headerRow.GetCell(i).StringCellValue.Trim() == ""))
                {
                    cellCount = i + 1;
                    break;
                }
                DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
                table.Columns.Add(column);
            }
            for (i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
            {
                HSSFRow row = (HSSFRow) sheet.GetRow(i);
                if (((row == null) || (row.GetCell(0) == null)) || (row.GetCell(0).ToString().Trim() == ""))
                {
                    break;
                }
                DataRow dataRow = table.NewRow();
                if (!string.IsNullOrEmpty(ownerID))
                {
                    dataRow[0] = ownerID;
                }
                if (!string.IsNullOrEmpty(customerID))
                {
                    dataRow[1] = customerID;
                }
                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) == null)
                    {
                        dataRow[j + colInc] = "";
                    }
                    else
                    {
                        string s = "";
                        if (row.GetCell(j).CellType == CellType.NUMERIC)
                        {
                            s = row.GetCell(j).NumericCellValue.ToString();
                        }
                        else
                        {
                            s = row.GetCell(j).ToString();
                        }
                        dataRow[j + colInc] = s;
                    }
                }
                table.Rows.Add(dataRow);
            }
            stream.Close();
            workbook = null;
            sheet = null;
            return table;
        }

        public DataTable XLS2DataTable(string fileName, string ownerID, string customerID)
        {
            using (FileStream stream = File.OpenRead(fileName))
            {
                int i;
                HSSFWorkbook workbook = new HSSFWorkbook(stream);
                HSSFSheet sheet = (HSSFSheet) workbook.GetSheetAt(0);
                HSSFRow headerRow = (HSSFRow) sheet.GetRow(0);
                DataTable table = new DataTable();
                int cellCount = headerRow.LastCellNum;
                int colInc = 0;
                if (!string.IsNullOrEmpty(ownerID))
                {
                    table.Columns.Add("货主");
                    colInc++;
                }
                if (!string.IsNullOrEmpty(customerID))
                {
                    table.Columns.Add("客户");
                    colInc++;
                }
                for (i = headerRow.FirstCellNum; i < cellCount; i++)
                {
                    if ((headerRow.GetCell(i) == null) || (headerRow.GetCell(i).StringCellValue.Trim() == ""))
                    {
                        cellCount = i + 1;
                        break;
                    }
                    DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
                    table.Columns.Add(column);
                }
                for (i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                {
                    HSSFRow row = (HSSFRow) sheet.GetRow(i);
                    if (((row == null) || (row.GetCell(0) == null)) || (row.GetCell(0).ToString().Trim() == ""))
                    {
                        break;
                    }
                    DataRow dataRow = table.NewRow();
                    if (!string.IsNullOrEmpty(ownerID))
                    {
                        dataRow[0] = ownerID;
                    }
                    if (!string.IsNullOrEmpty(customerID))
                    {
                        dataRow[1] = customerID;
                    }
                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        if (row.GetCell(j) == null)
                        {
                            dataRow[j + colInc] = "";
                        }
                        else
                        {
                            string s = "";
                            if (row.GetCell(j).CellType == CellType.NUMERIC)
                            {
                                s = row.GetCell(j).NumericCellValue.ToString();
                            }
                            else
                            {
                                s = row.GetCell(j).ToString();
                            }
                            dataRow[j + colInc] = s;
                        }
                    }
                    table.Rows.Add(dataRow);
                }
                stream.Close();
                workbook = null;
                sheet = null;
                return table;
            }
        }
    }
}

